開啟新專案與繪出遊戲起始場景方法皆相同,這裡不再詳細說明,如過程中有問題請參考[Day03] 簡介Visual Studio +XNA 與 [Day05] 剪刀石頭布猜拳遊戲Part1.繪出遊戲場景這兩篇文章
目標
可學到的東西
資源:
檔案 > 新增專案 > 範本 > Visual C# > Windows Game (4.0),將遊戲命名名為 GuessNumber1A2BMoocs
在遊戲直接調用資源都要先載入到開發環境中,不然有有可能出現錯誤,請特別注意。
這裡我們先載入開始畫面以及之後要用到的圖片。
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// 替每張圖片宣告一個變數
Texture2D imageNumber, imageGuess, imagePassGame;
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
// 載入變數,並對應每個圖片變數
imageNumber = Content.Load<Texture2D>("number640x256");
imageGuess = Content.Load<Texture2D>("guess256x128");
imagePassGame = Content.Load<Texture2D>("retry800x600");
}
在遊戲中要繪製圖型、字型等,只需要XNA提到的Sprite 即可,
不只可顯示於畫面上,還可以對圖片做放大、縮小等功能。
此 Sprite 是遊戲專案內建中,就是一開始就幫我們宣告的 SpriteBatch spriteBatch;
protected override void Initialize()
{
// TODO: Add your initialization logic here
// 設定起始畫面大小
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
graphics.ApplyChanges();
base.Initialize();
}
void drawing(SpriteBatch spriteBatch)
{
spriteBatch.Begin();
// sprite.draw 的使用方法很多種,這裡載入的只有3個參數
// 參數1表示要顯示的圖片,參數2表示顯示圖片的起始位置,參數3表示填色,White表示不填色
spriteBatch.Draw(imageNumber, Vector2.Zero, Color.White);
spriteBatch.Draw(imageGuess, new Vector2(400,400), Color.White);
spriteBatch.End();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.DarkViolet);
// TODO: Add your drawing code here
drawing(spriteBatch);
base.Draw(gameTime);
}
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace GuessNumber1A2BMoocs
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// 替每張圖片宣告一個變數
Texture2D imageNumber, imageGuess, imagePassGame;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
// 設定起始畫面大小
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
graphics.ApplyChanges();
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
// 載入變數,並對應每個圖片變數
imageNumber = Content.Load<Texture2D>("number640x256");
imageGuess = Content.Load<Texture2D>("guess256x128");
imagePassGame = Content.Load<Texture2D>("retry800x600");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.DarkViolet);
// TODO: Add your drawing code here
drawing(spriteBatch);
base.Draw(gameTime);
}
void drawing(SpriteBatch spriteBatch)
{
spriteBatch.Begin();
// sprite.draw 的使用方法很多種,這裡載入的只有3個參數
// 參數1表示要顯示的圖片,參數2表示顯示圖片的起始位置,參數3表示填色,White表示不填色
spriteBatch.Draw(imageNumber, Vector2.Zero, Color.White);
spriteBatch.Draw(imageGuess, new Vector2(400,400), Color.White);
spriteBatch.End();
}
}
}